package com.travel.controller;

import com.travel.config.SnowflakeIdWorker;
import com.travel.util.JedisPoolUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("order")
public class OrderController {
    

    private SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);

    @GetMapping("/createOrder")
    public String createOrder() {
        Jedis jedis = null;
        try {
            // 生成雪花id
            String token = String.valueOf(idWorker.nextId());

            JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
            jedis = jedisPoolInstance.getResource();
            
            // 将订单号存入redis
            jedis.setex(token, (int)TimeUnit.MINUTES.toSeconds(10), "unprocessed");

            return token;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
